<?php
	//数据规则验证类
	class validation
	{
		function validate( $field, $array )
		{
			$result = array();

			foreach( $field as $name => $data )
			{
				$function = 'validate_' . $data['type'];
				$min = isset( $data['min'] ) ? $data['min'] : '';
				$max = isset( $data['max'] ) ? $data['max'] : '';
				$other = isset( $data['other'] ) ? $data['other'] : '';

				if ( isset( $data['kind'] ) && $data['kind'] != '' )
				{
					if ( ! isset( $array[$data['kind']][$name] ) || ! $this->$function( $array[$data['kind']][$name], $min, $max, $other ) ) $result[] = $data['info'];
				}
				else
				{
					if ( ! isset( $array[$name] ) || ! $this->$function( $array[$name], $min, $max, $other ) ) $result[] = $data['info'];
				}
			}

			return $result;
		}

		function validate_num( $value, $min, $max, $other = '' )
		{
			$length = strlen( $value );
			return preg_match( '!^\d+$!', $value ) && $length >= $min && $length <= $max;
		}

		function validate_int( $value, $min, $max, $other = '' )
		{
			return preg_match( '!^\d+$!', $value ) && $value >= $min && $value <= $max;
		}

		function validate_float( $value, $min, $max, $other = '' )
		{
			return preg_match( '!^\d+(\.\d+)?$!', $value ) && $value >= $min && $value <= $max;
		}

		function validate_text( $value, $min, $max, $other = '' )
		{
			$length = strlen( $value );
			return $length >= $min && $length <= $max;
		}

		function validate_date( $value, $min, $max, $other = '' )
		{
			return preg_match( '/[0-9]{4}-[0-9]{2}-[0-9]{2}/', $value );
		}

		function validate_english( $value, $min, $max, $other = '' )
		{
			$length = strlen( $value );
			return preg_match( '/^[A-Za-z]+$/', $value ) && $length >= $min && $length <= $max;
		}

		function validate_chinese( $value, $min, $max, $other = '' )
		{
			$length = strlen( $value );
			return preg_match( '/^[' . chr( 0x81 ) . '-' . chr( 0xfe ) . ']+$/', $value ) && ( $length / 2 ) >= $min && ( $length / 2 ) <= $max;
		}

		function validate_ascii( $value, $min, $max, $other = '' )
		{
			$length = strlen( $value );
			return preg_match( '/[\x20-\x7f]+/', $value ) && $length >= $min && $length <= $max;
		}

		function validate_email( $value, $min, $max, $other = '' )
		{
			$length = strlen( $value );
			return preg_match( '/^[A-Za-z0-9]+([._\-\+]*[A-Za-z0-9]+)*@([A-Za-z0-9]+[-A-Za-z0-9]*[A-Za-z0-9]+\.)+[A-Za-z0-9]+$/', $value ) && $length >= $min && $length <= $max;
		}

		function validate_english_number( $value, $min, $max, $other = '' )
		{
			$length = strlen( $value );
			return preg_match( '/^[0-9]*[A-Za-z]+[A-Za-z0-9\.\-_]*$/', $value ) && $length >= $min && $length <= $max;
		}

		function validate_unixtime( $value, $min, $max, $other = '' )
		{
			return preg_match( '!^\d+$!', $value ) && strtotime( $value ) && $value >= $min && $value <= $max;
		}

		function validate_singel_select( $value, $min, $max, $other )
		{
			return in_array( $value, $other );
		}

		function validate_multi_select( $value, $min, $max, $other )
		{
			$result = 0;
			foreach( $value as $data ) if ( in_array( $data, $other ) ) $result++;
			return $result >= $min && $result <= $max;
		}
	}
?>